package com.ly.heart.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.dto.StartProcessDto;
import com.ly.heart.service.IActModelService;
import org.springframework.stereotype.Service;
import com.ly.heart.mapper.ProjectMapper;
import com.ly.heart.domain.ProjectEntity;
import com.ly.heart.service.IProjectService;

import javax.annotation.Resource;

/**
 * 项目Service业务层处理
 *
 * @author cxl
 * @date 2024-05-14
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper,ProjectEntity> implements IProjectService {

    @Resource
    private ProjectMapper projectMapper;
    @Resource
    private IActModelService iActModelService;

    /**
     * 查询项目
     *
     * @param id 项目主键
     * @return 项目
     */
    @Override
    public ProjectEntity selectProjectEntityById ( String id ) {
        return projectMapper.selectProjectEntityById ( id );
    }

    /**
     * 查询项目列表
     *
     * @param projectEntity 项目
     * @return 项目
     */
    @Override
    public List<ProjectEntity> selectProjectEntityList ( ProjectEntity projectEntity ) {
        return projectMapper.selectProjectEntityList ( projectEntity );
    }

    /**
     * 新增项目
     *
     * @param projectEntity 项目
     * @return 结果
     */
    @Override
    public int insertProjectEntity ( ProjectEntity projectEntity ) {
        String projectId = SnowflakeIdWorker.getInstance ().nextStringId ();
        StartProcessDto startDto = new StartProcessDto ().setBusinessId ( projectId ).
                setBusinessType ( projectEntity.getBusinessType () ).
                setModelId ( projectEntity.getModelId () ).
                setBusinessName ( "项目管理_".concat ( projectId ) );
        String instanceId = iActModelService.startProcess(startDto);
        projectEntity.setInstanceId(instanceId);
        projectEntity.setApprovalStatus ( ConstUtil.STATE_IN_PROCESS);
        projectEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        projectEntity.setCreateTime ( DateUtils.getNowDate () );
        return projectMapper.insertProjectEntity ( projectEntity );
    }

    /**
     * 修改项目
     *
     * @param projectEntity 项目
     * @return 结果
     */
    @Override
    public int updateProjectEntity ( ProjectEntity projectEntity ) {
        projectEntity.setUpdateTime ( DateUtils.getNowDate () );
        return projectMapper.updateProjectEntity ( projectEntity );
    }

    /**
     * 批量删除项目
     *
     * @param ids 需要删除的项目主键
     * @return 结果
     */
    @Override
    public int deleteProjectEntityByIds ( String[] ids ) {
        return projectMapper.deleteProjectEntityByIds ( ids );
    }

    /**
     * 删除项目信息
     *
     * @param id 项目主键
     * @return 结果
     */
    @Override
    public int deleteProjectEntityById ( String id ) {
        return projectMapper.deleteProjectEntityById ( id );
    }
}
